@using VocaDb.Model.DataContracts.PVs
@using VocaDb.Model.Domain.PVs
@using VocaDb.Model.Service.VideoServices
@inherits VocaDb.Web.Code.HelperPage

@helper EmbedNico(string pvId, string widthStr, string heightStr, string id = null, bool enableApi = false) {
	<div id="@id">
		<iframe width="@widthStr" height="@heightStr" allowfullscreen="allowfullscreen" style="border: none;" src="//embed.nicovideo.jp/watch/@pvId?jsapi=@(enableApi ? 1 : 0)&noRelatedVideo=0&autoplay=0&defaultNoComment=0&noLinkToNiconico=0&noController=0&noHeader=0&noTags=0&noShare=0" allow="autoplay; fullscreen"></iframe>	
	</div>
}

@helper EmbedPiapro(PVContract pv, string widthStr, string heightStr) {
	var meta = pv.ExtendedMetadata != null ? pv.ExtendedMetadata.GetExtendedMetadata<PiaproMetadata>() : null;
	if (meta != null && !string.IsNullOrEmpty(meta.Timestamp)) {
		var prefix = pv.PVId.Substring(0, 2);
		<audio controls controlsList="nodownload" src="https://cdn.piapro.jp/mp3_a/@prefix/@(pv.PVId)_@(meta.Timestamp)_audition.mp3"></audio>
	} else {
	@* <object> embed instead of iframe because iframe doesn't work with flash disabled *@
	var piaproSwf = string.Format("//piapro.jp/modpub/swf/player03_h.swf?030503&id={0}&c=1", pv.PVId);
	<object type="application/x-shockwave-flash" width="340" height="80" style="border: 0" data="@piaproSwf">
		<param name="movie" value="@piaproSwf" />
		<param name="quality" value="high"/>
	</object>
	}
}

@helper EmbedBili(PVContract pv, string widthStr, string heightStr) {
	var meta = pv.ExtendedMetadata != null ? pv.ExtendedMetadata.GetExtendedMetadata<BiliMetadata>() : null;
	if (meta != null && meta.Cid != 0) {
		<iframe src="https://player.bilibili.com/player.html?aid=@pv.PVId&cid=@meta.Cid&page=1" scrolling="no" border="0" frameborder="no" framespacing="0" allowfullscreen="true" width="@widthStr" height="@heightStr"></iframe>
	} else {
		<embed height="@heightStr" width="@widthStr" quality="high" allowfullscreen="true" type="application/x-shockwave-flash" src="https://static.hdslb.com/miniloader.swf" flashvars="aid=@pv.PVId&page=1" pluginspage="https://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" />
	}
}

@helper EmbedPV(PVContract pv, int width = 560, int height = 315, bool autoplay = false, bool enableApi = false, string id = null) {
	var widthStr = (width > 0 ? width.ToString() : "");
	var heightStr = (height > 0 ? height.ToString() : "");
	id = id ?? string.Empty;

	switch (pv.Service) {
		case PVService.Bandcamp:
			<iframe style="border: 0; width: 100%; height: 120px;" src="https://bandcamp.com/EmbeddedPlayer/size=large/bgcol=ffffff/linkcol=0687f5/tracklist=false/artwork=small/track=@(pv.PVId)/transparent=true/" seamless></iframe>
			break;

		case PVService.Bilibili:
			@EmbedBili(pv, widthStr, heightStr)
			break;

		case PVService.File:
		case PVService.LocalFile:
			if (LocalFileManager.IsAudio(pv.Url)) {
				<audio id="@id" controls controlsList="nodownload" src="@pv.Url"></audio>
			} else {				
				<div style="width: @(widthStr + "px"); height: @(heightStr + "px")">
					<a href="@pv.Url">
						<img style="max-width: 100%; max-height: 100%;" src="@pv.ThumbUrl" alt="@pv.Name" />
					</a>
				</div>				
			}
			break;

		case PVService.NicoNicoDouga:
			@EmbedNico(pv.PVId, widthStr, heightStr, id, enableApi)
			break;

		case PVService.Piapro:
			@EmbedPiapro(pv, widthStr, heightStr)
		break;

		case PVService.SoundCloud:
			var scId = new VocaDb.Model.Service.VideoServices.SoundCloudId(pv.PVId);
				<iframe id="@id" width="@width" height="@Math.Min(height, 166)" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?url=https%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F@(scId.TrackId)&amp;auto_play=false&amp;show_artwork=true&amp;color=ff7700"></iframe>
				break;

		case PVService.Youtube:
			var apParam = (autoplay ? 1 : 0);
			var apiParam = (enableApi ? 1 : 0);
			var originParam = (enableApi ? VocaDb.Model.Utils.VocaUriBuilder.HostAddress : string.Empty);
			<iframe id="@id" width="@widthStr" height="@heightStr" src="https://www.youtube.com/embed/@(pv.PVId)?autoplay=@apParam&enablejsapi=@apiParam&origin=@originParam" frameborder="0" wmode="Opaque" allowfullscreen></iframe>
			break;

		case PVService.Vimeo:
			<iframe src="https://player.vimeo.com/video/@pv.PVId" width="@width" height="@height" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>
		break;

		case PVService.Creofuga:
			<iframe width="@widthStr" height="@Math.Min(height, 120)" scrolling="no" frameborder="no" src="https://creofuga.net/audios/player?color=black&id=@pv.PVId"></iframe>
		break;
	}

}
